<template>
 <div class="tiku-container">
    <div class="app">
      <el-card>
      <el-form  label-width="80px" :inline="true" size="normal">
        <el-row type="flex" justify="space-between">
          <el-col :span="4">
            <el-form-item label="关键字" style="width:300px">
          <el-input v-model="queryData.keyword"></el-input>
        </el-form-item>
          </el-col>

          <el-col :span="4" justify="end">
             <el-button size="small" type="primary" @click="initData">搜索</el-button>
          <el-button size="small" @click="clearFn">清除</el-button>
          </el-col>
        </el-row>
      </el-form>
      

       <el-alert
      :title="`共计${counts}条`"
      type="info"
      show-icon
      :closable="false"
    >
    </el-alert>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="number" label="编号" width="250">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" width="120">
      </el-table-column>
      <el-table-column prop="questionNumber" label="题目编号" width="120">
        <template slot-scope="{row}">
          <span @click="prevDialog(row.id)">{{row.questionNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="120">
      </el-table-column>
      <el-table-column prop="timea" label="答题时间" width="120">  
      </el-table-column>
      <el-table-column prop="anwser" label="正确率(%)" width="120">
      </el-table-column>
      <el-table-column prop="name" label="录入人" width="120">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">   
        <template slot-scope="{row}">
          <el-button  type="danger" icon="el-icon-delete" circle size="small" @click="delFn(row.number)"></el-button>
        </template>
      </el-table-column>  
    </el-table>

      </el-card>


       <el-pagination
      align="right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.page"
      :page-sizes="[3, 4, 5, 10]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="counts"
    >
    </el-pagination>

    <!-- 弹框组件 -->
    <questions-info ref="quDialog"></questions-info>
    </div>
 </div>
</template>

<script>
import {getRamdomListApi, delRamdomListApi , previewListApi} from '@/api/randoms'
import questionsInfo from '../choice/components/questions-info.vue'
export default {
  components: { questionsInfo },
  name: 'ListPage',
  data(){
    return{
      list:[],
      counts:'',
      tableData:[],
      queryData:{
         page:1,
        pagesize: 10,
        keyword:''
      }
    }
  },
  created(){
    this.initData()
  },
  methods:{
    async initData(){
     const res = await getRamdomListApi(this.queryData)
    
     this.tableData = res.items
     console.log(this.tableData);
     this.counts = res.count
     console.log(this.counts);
    //  this.list = res.items
    },

   async delFn(number){
    await this.$confirm('确定删除吗 ？')
    await delRamdomListApi({
      number
      })
      this.$message.success('删除成功')
        // 边缘问题，如果不是第一页，且这一页只有一个且删除成功，则页码要-1
      if (this.queryData.page !== 1 && this.tableData.length === 1) {
      this.queryData.page--
      }
      this.initData()
    },

    clearFn(){
      this.queryData.keyword=''
      this.initData()
    },

   async prevDialog(val){
      this.$refs.quDialog.showDialog = true
      // this.$refs.quDialog.id = val
      const res = await previewListApi(val)

      this.$refs.quDialog.info = res
    },



    // 每页数量切换事件【列表功能】
    handleSizeChange(val) {
      // val是最新的每页数量
      console.log(`每页 ${val} 条`);
      this.queryData.pagesize = val;
      this.$nextTick(() => {
        this.initData();
      });
    },
    //页码切换事件【列表功能】
    handleCurrentChange(val) {
      // val是最新的页码
      console.log(`当前页: ${val}`);
      this.queryData.page = val;
      this.initData();
    },
  }
}
</script>

<style>
.el-form-item__label {
  font-size: 12px;
}

.el-dialog__header {
  background: #409eff
}
</style>
